<?php


namespace backend\controllers;


use common\helpers\TransformApi;
use common\models\Address;
use common\models\Order;
use common\models\User;
use common\models\VipLog;
use Yii;
/**
 * 用户控制器
 * Class UserController
 * @package backend\controllers
 */
class UserController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = User::className();
    }

    /**
     * 列表
     * @return array|string
     */
    public function actionIndex()
    {
        if (Yii::$app->request->isPost) {
            $where = [];
            $data = parent::actionIndex(true, $where);
            foreach ($data['data'] as &$datum){
                $info = json_decode($datum['info'],true);
                $datum['nickname'] = $info['nickName'];
                $datum['avatar'] = $info['avatarUrl'];
                $huiyuan = (new User())->isHuiyuan($datum['id'],$datum['start_time'],$datum['end_time'],1);
                $datum['huiyuan'] = '<span class="label label-warning">否</span>';
                if ($huiyuan){
                    $datum['huiyuan'] = '<span class="label label-warning">是</span>'.$datum['end_time'].'到期';
                }
            }
            return $data;
        } else {
            return parent::actionIndex();
        }
    }

    public function actionDetail()
    {
        $id = Yii::$app->request->get('id','');
        $model = User::find()->where(['id'=>$id])->one();
        if (!$model){
            return $this->bError('暂无该用户');
        }
        $info = json_decode($model->info,true);
        $model->nickname = $info['nickName'];
        $model->headimgurl = $info['avatarUrl'];
        $start_month = date('Y-m-01 00:00:00');
        $end_month = date('Y-m-t 23:59:59');
        $month = Order::getMarketData($start_month,$end_month,$id);
        $total = Order::getMarketData('','',$id);
        return $this->render('',['model'=>$model,'id'=>$id,'month'=>$month,'total'=>$total]);
    }

    public function actionAddress()
    {
        $this->model = Address::className();
        if (Yii::$app->request->isPost) {
            $userid = Yii::$app->request->get('userid','');
            return parent::actionIndex(true,['userid'=>$userid]);
        } else {
            return parent::actionIndex();
        }
    }

    /**
     * 订单列表
     * @return array|string
     */
    public function actionOrder()
    {
        $this->model = Order::className();
        if (Yii::$app->request->isPost) {
            $userid = Yii::$app->request->get('userid','');
            return parent::actionIndex(true,['and',['userid'=>$userid],['!=','pay_status',0]]);
        } else {
            return parent::actionIndex();
        }
    }

    public function actionVipLog()
    {
        $this->model = VipLog::className();
        if (Yii::$app->request->isPost) {
            $userid = Yii::$app->request->get('userid','');
            return parent::actionIndex(true,['and',['userid'=>$userid],['status'=>1]]);
        } else {
            return parent::actionIndex();
        }
    }
}